package algorithm;

import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bigOrang
 * @date 2021/9/12
 * xxx
 */
public class StackToQueue {

    @Test
    public void test() throws InterruptedException {

        MyQueue myStack = new MyQueue();

        for (int i = 0; i < 1000; i++) {
            try {
                myStack.put(i);
                //System.out.println("put " + i);
                if (i % 3 == 0) {
                    //System.out.println("poll" + myStack.poll());
                }

            } catch (Exception e) {
                e.printStackTrace();
                Object obj = null;
                while ((obj = myStack.poll()) != null) {
                    System.out.println(obj);
                }
                break;
            }

        }

    }


}

@Data
class MyQueue {

    static List<Object> stack1 = new ArrayList<>(10);
    static List<Object> stack2 = new ArrayList<>(10);

    private Object i;


    public void put(Object v) throws InterruptedException {
        if (stack1.size() + stack2.size() >= 10) {
            throw new RuntimeException("full");
        }

        if (stack1.isEmpty() && stack2.isEmpty()) {
            stack1.add(v);
        } else if (!stack1.isEmpty()) {
            stack2.addAll(stack1);
            stack2.add(v);
            stack1.clear();
        } else {
            stack1.addAll(stack2);
            stack1.add(v);
            stack2.clear();
        }
    }

    public Object poll() {
        if (!stack1.isEmpty()) {
            Object v = stack1.get(0);
            stack1.remove(0);
            return v;

        }
        if (!stack2.isEmpty()) {
            Object v = stack2.get(0);
            stack2.remove(0);
            return v;

        }
        return null;
    }


}
